package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyContract;
import cn.stylefeng.guns.sys.modular.system.entity.BtyContractDetail;
import cn.stylefeng.guns.sys.modular.system.entity.BtyContractOrtherDetail;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyContractParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyContractResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ReadonlyResult;
import cn.stylefeng.guns.sys.modular.system.service.AreaService;
import cn.stylefeng.guns.sys.modular.system.service.BtyContractDetailService;
import cn.stylefeng.guns.sys.modular.system.service.BtyContractOrtherDetailService;
import cn.stylefeng.guns.sys.modular.system.service.BtyContractService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 合同管理控制器
 *
 * @author liuy
 * @Date 2022-05-10 16:16:40
 */
@Controller
@RequestMapping("/btyContract")
public class BtyContractController extends BaseController {

    private String PREFIX = "/modular/system/btyContract";

    @Autowired
    private BtyContractService btyContractService;
    @Resource
    private AreaService areaService;
    @Resource
    private BtyContractDetailService detailService;
    @Resource
    private BtyContractOrtherDetailService ortherDetailService;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyContract.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyContract_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyContract_edit.html";
    }

    /**
     * 新增基本信息接口
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(@RequestBody(required = false) BtyContractParam btyContractParam) {
        Integer id;
        if(ToolUtil.isNotEmpty(btyContractParam)&&ToolUtil.isNotEmpty(btyContractParam.getDm())) {
           //根据店名查询该店是否已经存在
           List<BtyContract> list=btyContractService.list(new QueryWrapper<BtyContract>().eq("dm",btyContractParam.getDm()));
           if(list.size()>0){
               return ResponseData.error(502,btyContractParam.getDm()+"店已存在,不允许重复添加！");
           }
        }
        if(ToolUtil.isNotEmpty(btyContractParam)){
            id=btyContractService.add(btyContractParam);
        }
        else{
            return ResponseData.error(501,"参数不能为空！");
        }
        return ResponseData.success(id);
    }

    /**
     * 新增租金明细接口
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("/addRentdetails")
    @ResponseBody
    public ResponseData addRentdetails(@RequestBody(required = false) BtyContractParam btyContractParam) {
        if(ToolUtil.isNotEmpty(btyContractParam)){
            this.btyContractService.add(btyContractParam);
        }else{
            return ResponseData.error(501,"参数不能为空！");
        }
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(@RequestBody(required = false)BtyContractParam btyContractParam) {
        if(ToolUtil.isNotEmpty(btyContractParam)){
            this.btyContractService.update(btyContractParam);
        }else{
            return ResponseData.error(501,"参数不能为空！");
        }
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyContractParam btyContractParam) {
        this.btyContractService.delete(btyContractParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyContractParam btyContractParam) {
        BtyContract detail = this.btyContractService.getById(btyContractParam.getId());
        List<BtyContractDetail> detailList=detailService.list(new QueryWrapper<BtyContractDetail>().eq("contract_id",btyContractParam.getId()));
        detail.setDetailList(detailList);
        List<BtyContractOrtherDetail> otherDetailList=ortherDetailService.list(new QueryWrapper<BtyContractOrtherDetail>().eq("contract_id",btyContractParam.getId()));
        detail.setOtherDetailList(otherDetailList);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2022-05-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyContractParam btyContractParam) throws ParseException {
        return this.btyContractService.findPageBySpec(btyContractParam);
    }

    /**
     * 查询一级区域列表
     */
    @RequestMapping(value = "/primaryAreaList")
    @ResponseBody
    public List<Map> primaryAreaList() {
        List<Map> list = btyContractService.primaryAreaList();
        return list;
    }
    /**
     * 根据pid查找所有对应的子级区域
     *
     */
    @RequestMapping(value = "/secondaryAreaList")
    @ResponseBody
    public List<Map> secondaryAreaList(int pid) {
        List<Map> list = btyContractService.secondaryAreaList(pid);
        return list;
    }

    /**
     * 获取开发人员列表
     * @return
     */
    @RequestMapping(value = "/kfryList")
    @ResponseBody
    public List<Map> kfryList() {
        List<Map>kfryList=btyContractService.kfryList();
        return kfryList;
    }

    /**
     * 获取店名列表
     * @return
     */
    @RequestMapping(value = "/dmList")
    @ResponseBody
    public List<Map> dmList() {
        List<Map>list=btyContractService.dmList();
        return list;
    }

    /**
     * 获取导出excel的数据
     * @param param
     * @return
     */
    @RequestMapping("/listSearchExport")
    @ResponseBody
    public List<BtyContractResult> listSearchExport (BtyContractParam param) throws ParseException {
        List<BtyContractResult> list= btyContractService.findListBySpec(param);
        return list;
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUser(@RequestParam("file") MultipartFile file, BtyContractParam param) {
        String fileName = file.getOriginalFilename();
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        try {
            flag = btyContractService.batchImport(fileName, file,param);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;
    }

    @RequestMapping("/export")
    public ResponseEntity<byte[]> export(HttpServletRequest request, HttpServletResponse response, BtyContractParam param) {
        return this.btyContractService.exportMx(request, response, param);
    }

    @RequestMapping("/readonlyList")
    @ResponseBody
    public ReadonlyResult readonlyList (){
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        String username=LoginContextHolder.getContext().getUser().getName();
        ReadonlyResult result=new ReadonlyResult();
        if(username.equals("丁飞")){
            result.setReadonly(2);
        }
        if(roleNames.contains("成本会计")){
            result.setCbkjFlag(2);
        }
        if(roleNames.contains("超级管理员")){
            result.setCjglFlag(2);
        }
        return result;
    }

}


